<template>
  <div class="login-container">
      <!-- 导航栏 -->
      <van-nav-bar  title="管理员登录" >
        <van-icon slot="left" name="cross"  @click="$router.back()"/>
      </van-nav-bar>
      <!-- 登陆表单 -->
      <van-form ref="loginForm" @submit="onSubmit">
        <van-field
          v-model="userAdmin.username"

          type="string"
          name="username"
          label="帐户名"
          placeholder="请输入登录帐号"
          :rules="userFormRules.mobile"
          maxlength="11"
        />
        <van-field
        v-model="userAdmin.password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="userFormRules.code"
          type="number"
          maxlength="6"
        >

      </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn"  block type="info" native-type="submit">登录</van-button>

        </div>

      </van-form>
  </div>
</template>

<script>
import { adminLogin } from '@/api/user'
export default {
  name: 'userLoginIndex',
  data () {
    return {
      show: false, // 验证弹窗
      userAdmin: {
        username: 'hehe', // 账户名
        password: '123456' // 验证码
      },

      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, {
          // 增加规则
          // pattern: /^1[3|5|7|8]\d{9}$/,
          // message: '手机号格式错误'
        }],
        code: [{ required: true, message: '密码不能为空' }
          // {
          //   pattern: /^\d{6}$/,
          //   message: '验证码格式错误'
          // }
        ]
      }
      // isCountDownShow: false
    }
  },
  methods: {
    async onSubmit () {
      // 1.获取表单数据
      const userAdmin = this.userAdmin
      // 2.表单验证
      // 在组件中必须通过this.$toast来调用toast
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 如果为0，则持续展示
        // loadingType: 'spinner'
      })
      // 3.提交表单请求登录
      try {
        const { data: { data: res } } = await adminLogin(userAdmin)
        console.log(res)
        if (res.username === userAdmin.username && res.password === userAdmin.password) {
          this.$toast.success('登录成功')
          // 登陆成功，跳转回原来页面
          this.$router.push('/my')
        }
        this.$store.commit('setUser', res)
      } catch (error) {
        this.$toast.fail('登陆失败,请稍后重试', error)
      }
    }

  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #3926fa;
  /deep/ .van-nav-bar__title {
    color: #fff;
  }
}

.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }

}
</style>
